using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace LibGreen.Jabber.ProtocolElements
{
    /// <summary>
    /// Describes a &lt;stream:stream&gt; element
    /// </summary>
    public class StreamElement
    {
        /// <summary>
        /// The xmlns of the &lt;stream:stream&gt; element
        /// </summary>
        private const string XMLNS = "jabber:client";

        /// <summary>
        /// Creates a new &lt;stream:stream&gt; element originating from the client
        /// </summary>
        /// <param name="to">The domain of the receiving entity</param>
        /// <returns>A new &lt;stream:stream&gt; element</returns>
        public static Element Create(string to)
        {
            Dictionary<string, string> attributes = new Dictionary<string, string>(5);
            attributes["to"] = to;
            attributes["xmlns"] = XMLNS;
            attributes["xmlns:stream"] = "http://etherx.jabber.org/streams";
            attributes["version"] = "1.0";
            return Element.CreateElement("stream:stream", attributes);
        }

        private readonly string from;
        private readonly string id;

        /// <summary>
        /// Initializes a StreamElement from a received <see cref="Element"/>
        /// </summary>
        /// <param name="element">The received <see cref="Element"/> containing the &lt;stream:stream&gt; stanza</param>
        public StreamElement(Element element)
        {
            this.from = element.Attributes["from"];
            this.id = element.Attributes["id"];
        }

        /// <summary>
        /// Gets the domain as set by the server
        /// </summary>
        public string From
        {
            get { return from; }
        }

        /// <summary>
        /// Gets the session ID set by the server
        /// </summary>
        public string SessionID
        {
            get { return id; }
        }
    }
}
